dialog: Use default decoration for non-custom headerbars
authorFlorian Müllner <fmuellner@gnome.org>
Sun, 12 May 2019 09:58:22 +0000 (09:58 +0000)
committerFlorian Müllner <fmuellner@gnome.org>
Sun, 12 May 2019 20:09:03 +0000 (20:09 +0000)
There are two ways GTK can add a headerbar to a dialog:

 - the dialog is constructed with the :use-header-bar property

 - all windows should use client-side decorations

In the first case, the headerbar is added by GtkDialog with no
dedicated style class, and in the latter by GtkWindow with the
"default-decoration" style.

As a result, dialogs with plain titlebars can end up with clearly
distinct and inconsistent styles.

To address this, allow headerbars to track whether they should use
the "default-decoration" style and enable it for dialogs.

https://gitlab.gnome.org/GNOME/gtk/merge_requests/836

gtk/gtkdialog.c
gtk/gtkheaderbar.c
gtk/gtkheaderbarprivate.h
gtk/ui/gtkdialog.ui

index 41f71a233a134324d8a38dcb55d9e4caa4b91355..8b0e7faf442703ec5c36f91533159cc2cab4c01f 100644 (file)
@@ -31,6 +31,7 @@
 #include "gtkdialog.h"
 #include "gtkdialogprivate.h"
 #include "gtkheaderbar.h"
+#include "gtkheaderbarprivate.h"
 #include "gtklabel.h"
 #include "gtkmarshalers.h"
 #include "gtkbox.h"
@@ -460,6 +461,7 @@ gtk_dialog_constructed (GObject *object)
       g_list_free (children);
 
       update_suggested_action (dialog);
+      _gtk_header_bar_track_default_decoration (GTK_HEADER_BAR (priv->headerbar));
 
       g_signal_connect (priv->action_area, "add", G_CALLBACK (add_cb), dialog);
     }
@@ -1549,6 +1551,7 @@ gtk_dialog_buildable_add_child (GtkBuildable  *buildable,
   else if (g_str_equal (type, "titlebar"))
     {
       priv->headerbar = GTK_WIDGET (child);
+      _gtk_header_bar_track_default_decoration (GTK_HEADER_BAR (priv->headerbar));
       gtk_window_set_titlebar (GTK_WINDOW (buildable), priv->headerbar);
     }
   else if (g_str_equal (type, "action"))
index dcdcb4f4fda376809207109c8b80adca7b981d5d..aca524df1052da65d71cc6eac454873c9b86ff79 100644 (file)
@@ -85,6 +85,7 @@ struct _GtkHeaderBarPrivate
   gboolean show_title_buttons;
   gchar *decoration_layout;
   gboolean decoration_layout_set;
+  gboolean track_default_decoration;
 
   GtkWidget *titlebar_start_box;
   GtkWidget *titlebar_end_box;
@@ -499,6 +500,30 @@ _gtk_header_bar_shows_app_menu (GtkHeaderBar *bar)
   return priv->show_title_buttons && priv->shows_app_menu;
 }
 
+static void
+update_default_decoration (GtkHeaderBar *bar)
+{
+  GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (bar);
+  GtkStyleContext *context;
+
+  context = gtk_widget_get_style_context (GTK_WIDGET (bar));
+
+  if (priv->children != NULL || priv->custom_title != NULL)
+    gtk_style_context_remove_class (context, "default-decoration");
+  else
+    gtk_style_context_add_class (context, "default-decoration");
+}
+
+void
+_gtk_header_bar_track_default_decoration (GtkHeaderBar *bar)
+{
+  GtkHeaderBarPrivate *priv = gtk_header_bar_get_instance_private (bar);
+
+  priv->track_default_decoration = TRUE;
+
+  update_default_decoration (bar);
+}
+
 /* As an intended side effect, this function allows @child
  * to be the title/label box */
 static void
@@ -1543,6 +1568,9 @@ gtk_header_bar_pack (GtkHeaderBar *bar,
   g_signal_connect (widget, "notify::visible", G_CALLBACK (notify_child_cb), bar);
 
   _gtk_header_bar_update_separator_visibility (bar);
+
+  if (priv->track_default_decoration)
+    update_default_decoration (bar);
 }
 
 static void
@@ -1595,6 +1623,9 @@ gtk_header_bar_remove (GtkContainer *container,
       g_free (child);
       gtk_widget_queue_resize (GTK_WIDGET (container));
       _gtk_header_bar_update_separator_visibility (bar);
+
+      if (priv->track_default_decoration)
+        update_default_decoration (bar);
     }
 }
 
index 5ace00e37b4a47a159a013411e8556373a013c1a..bad342d5fdf592d634375a0d59934c43d59968ec 100644 (file)
@@ -25,6 +25,7 @@
 G_BEGIN_DECLS
 
 gboolean     _gtk_header_bar_shows_app_menu        (GtkHeaderBar *bar);
+void         _gtk_header_bar_track_default_decoration (GtkHeaderBar *bar);
 void         _gtk_header_bar_update_window_buttons (GtkHeaderBar *bar);
 gboolean     _gtk_header_bar_update_window_icon    (GtkHeaderBar *bar,
                                                     GtkWindow    *window);
index 9c83c84207d47b3aca1830ef88ae75584fcc8329..6b427e4f0bcd42cc8ebbaf921c2dd2a98094eef1 100644 (file)
@@ -6,6 +6,7 @@
     <child type="titlebar">
       <object class="GtkHeaderBar" id="headerbar">
         <property name="show-title-buttons">1</property>
+        <property name="has-subtitle">false</property>
       </object>
     </child>
     <child>